package juego.niveles.generator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import juego.entidades.Entidad;
import juego.niveles.MatrixLevel;
import juego.niveles.Nivel;



public class CreatorByFile extends Creator {
	
	protected Nivel level;
	protected CharacterMap map;
	
	public CreatorByFile( File f )
	{
		this.level = new MatrixLevel();
		this.map = new CharacterMap();
		this.parseFile(f);
	}
	
	
	public Nivel getLevel() {
		return level;
	}
	
	protected void parseFile( File f )
	{
		int line = 0;
		try {
		    BufferedReader in = new BufferedReader(new FileReader(f));
		    String str;
		    while ((str = in.readLine()) != null) {
		        this.parseLine(line, str); 
		        line++;
		    }
		    in.close();
		} catch (IOException e) {
		}
	}
	
	protected void parseLine(int line, String str )
	{
		for( int i = 0; i < str.length(); i++ )
		{
			Character c = new Character(str.charAt(i));
			Entidad e = this.map.get(c);
			if (e!=null){
			Entidad x = (Entidad)e.clonar();
			this.level.setEntidad(i, line, x);}
		}
	}

}
